package com.owulia.leetcode.num;

import com.alibaba.fastjson.JSONObject;

public class MoveZeroes {

    public static void moveZeroes(int[] nums) {
        int length = nums.length - 1;
        for (int i = 0; i < length; i++) {
            if (nums[i] == 0) {
                for (int j = i; j < length; j++) {
                    nums[j] = nums[j + 1];
                }
                nums[length] = 0;
                i--;
                length--;
            }
            System.out.println(JSONObject.toJSONString(nums));
        }
    }

    public static void moveZeroesV2(int[] nums) {
        int length = nums.length;
        int i = 0;
        int j = 0;
        while (j < length) {
            if (nums[j] != 0) {
                nums[i++] = nums[j];
            }
            ++j;
            System.out.println(JSONObject.toJSONString(nums));
        }
        for (int k = i; k < length; k++) {
            nums[k] = 0;
        }
        System.out.println(JSONObject.toJSONString(nums));

    }

    public static void main(String[] args) {
        int[] nums = {0, 1, 0, 3, 12};
        moveZeroesV2(nums);
    }
}
